/*
Copyright (c)2009 Seesmic, Inc

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package com.seesmic.twhix
{
	public class PresenceStanza extends Stanza
	{
		public var from:JID = new JID();
		public var to:JID = new JID();
		public var type:String = 'available';
		public var status:String = '';
		public var priority:String = '0';
		public var category:String = 'available';
		namespace jc = 'jabber:client';
		default xml namespace = 'jabber:client';
		
		
		public function PresenceStanza(connection:Object, parent:Stanza=null)
		{
			super(connection, parent);
		}
		
		override public function fromXML(inxml:XML):void {
			super.fromXML(inxml);
			default xml namespace = "jabber:client";
			from.fromString(xml.@from);
			to.fromString(xml.@to);
			this.type = xml.@type;
			this.status = xml.status.text();
			this.priority = xml.priority.text();
			if(!this.type) {
				this.type = xml.show.text();
			}
			if(!this.type) {
				this.type = 'available';
			}
		}
		
		public function getCategory():String {
			var available:Array = new Array('available', 'away', 'ffc', 'dnd', 'na');
			var subscribe:Array = new Array('subscribe', 'subscribed', 'unsubscribe');
			if(type == 'unavailable') {
				return 'unavailable';
			} else if (available.indexOf(type) != -1) {
				return 'available';
			} else if (subscribe.indexOf(type) != -1) {
				return 'subscribe';
			} else if (type == 'error') {
				return 'error';
			}
			return 'unknown';
		}
		
		public function setTo(nto:String):void {
			this.to.fromString(nto);
		}
		
		public function setFrom(nfrom:String):void {
			this.from.fromString(nfrom);
		}
		
		public function setStatus(nstatus:String):void {
			this.status = nstatus;
		}
		
		public function setPriority(npriority:String):void {
			this.priority = npriority;
		}
		
		public function setType(ntype:String): void {
			// TODO: verify type is valid;
			this.type = ntype;
		}
		
		override public function render():void {
			xml = <presence />;
			//xml.setNamespace(jc);
			xml.@from = conn.fulljid.toString();
			if(this.to && this.to.isSet()) {
				xml.@to = to.toString();
			}
			if(this.type == 'unavailable') {
				xml.@type = 'unavailable';
			} else if (this.type && this.type != 'available') {
				var showx:XML = <show>{type}</show>;
				xml.appendChild(showx);
			}
			if(this.priority) {
				var priorityx:XML = <priority>{priority}</priority>;
				xml.appendChild(priorityx);
			}
			if(this.status) {
				var statusx:XML = <status>{status}</status>;
				xml.appendChild(statusx);
			}
		}
				
	}
}
